package com.newhero.product.module.sys.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.mybatis.plugins.pagination.Pagination;
import com.newhero.product.common.service.impl.BaseServiceImpl;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.module.sys.mapper.RoleMapper;
import com.newhero.product.module.sys.mapper.RoleAppMapper;
import com.newhero.product.module.sys.mapper.UserRoleMapper;
import com.newhero.product.module.sys.model.Role;
import com.newhero.product.module.sys.model.RoleApp;
import com.newhero.product.module.sys.model.UserRole;
import com.newhero.product.module.sys.model.vo.RoleVo;
import com.newhero.product.module.sys.service.IRoleService;


/**
 *
 * Role 表数据服务层接口实现类
 *
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleAppMapper roleAppMapper;
    
    @Override
    public Map<String, Set<String>> selectAppMapByUserID(String userID) {
        Map<String, Set<String>> resourceMap = new HashMap<String, Set<String>>();
        List<String> roleIdList = userRoleMapper.selectRoleIdListByUserId(userID);
        Set<String> urlSet = new HashSet<String>();
        Set<String> roles = new HashSet<String>();
        for (String roleId : roleIdList) {
            List<Map<String, String>> resourceList = roleMapper.selectAppListByRoleID(roleId);
            if (resourceList != null) {
                for (Map<String, String> map : resourceList) {
                    if (StringUtils.isNotBlank(map.get("url"))) {
                        urlSet.add(map.get("url"));
                    }
                }
            }
            Role role = roleMapper.selectById(roleId);
            if (role != null) {
                roles.add(role.getRoleName());
            }
        }
        resourceMap.put("urls", urlSet);
        resourceMap.put("roles", roles);
        return resourceMap;
    }
    /*
     * (non-Javadoc)
     * @see com.newhero.product.module.sys.service.IRoleService#setIfSetted(java.lang.String, java.lang.String)
     */
	@Override
	public boolean setIfSetted(String appID, String roleId) {
		EntityWrapper<RoleApp> wrapper = new EntityWrapper<RoleApp>();
		wrapper.where("roleID={0} and appID={1}", roleId, appID);
		int result = roleAppMapper.selectCount(wrapper);
		if(result > 0){
			//说明设置了
			return true;
		}else{
			return false;
		}
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IAppService#addRoleAppData(java.lang.String, java.lang.String)
	 */
	@Override
	public void addRoleAppData(String roleId, String appID) {
		RoleApp entity = new RoleApp();
		entity.setAppID(appID);
		entity.setRoleID(roleId);
		roleAppMapper.insert(entity);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IAppService#removeRoleAppData(java.lang.String, java.lang.String)
	 */
	@Override
	public void removeRoleAppData(String roleId, String appID) {
		Map<String,Object> columnMap = new HashMap<String,Object>();
		columnMap.put("roleID", roleId);
		columnMap.put("appID", appID);
		roleAppMapper.deleteByMap(columnMap);
	}
	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IRoleService#addUserRoleData(java.lang.String, java.lang.String)
	 */
	@Override
	public void addUserRoleData(String userId, String roleId) {
		UserRole entity = new UserRole();
		entity.setUserID(userId);
		entity.setRoleID(roleId);
		userRoleMapper.insert(entity);
	}
	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IRoleService#removeUserRoleData(java.lang.String, java.lang.String)
	 */
	@Override
	public void removeUserRoleData(String userId, String roleId) {
		Map<String,Object> columnMap = new HashMap<String,Object>();
		columnMap.put("roleID", roleId);
		columnMap.put("userID", userId);
		userRoleMapper.deleteByMap(columnMap);
	}
	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IRoleService#selectRoleNameSetByUserID(java.lang.String)
	 */
	@Override
	public Set<String> selectRoleNameSetByUserID(String userID) {
		Map<String,Object> columnMap = new HashMap<String,Object>();
		columnMap.put("userID", userID);
		List<UserRole> userRoleList = userRoleMapper.selectByMap(columnMap);
		Set<String> roleNameSet = new HashSet<String>();
		for(UserRole ur : userRoleList){
			roleNameSet.add(ur.getRoleID());
		}
		return roleNameSet;
	}
	@Override
	public List<RoleVo> selectRoleListPage(Pagination page, Map<String, Object> params) {
		return roleMapper.selectRoleListPage(page, params);
	}
}